
/****************************************************************************/
/*Copyright (c) 2011, Florent DEVILLE.                                      */
/*All rights reserved.                                                      */
/*                                                                          */
/*Redistribution and use in source and binary forms, with or without        */
/*modification, are permitted provided that the following conditions        */
/*are met:                                                                  */
/*                                                                          */
/* - Redistributions of source code must retain the above copyright         */
/*notice, this list of conditions and the following disclaimer.             */
/* - Redistributions in binary form must reproduce the above                */
/*copyright notice, this list of conditions and the following               */
/*disclaimer in the documentation and/or other materials provided           */
/*with the distribution.                                                    */
/* - The names of its contributors cannot be used to endorse or promote     */
/*products derived from this software without specific prior written        */
/*permission.                                                               */
/* - The source code cannot be used for commercial purposes without         */ 
/*its contributors' permission.                                             */
/*                                                                          */
/*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       */
/*"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT         */
/*LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS         */
/*FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE            */
/*COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,       */
/*INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,      */
/*BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;          */
/*LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER          */
/*CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT        */
/*LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN         */
/*ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE           */
/*POSSIBILITY OF SUCH DAMAGE.                                               */
/****************************************************************************/

#pragma once

#include "RTSource.h"
#include "RTTypes.h"

namespace RT
{
	/*RTParameter class.							*/
	/*This class is used to link between controller */
	/*as a parameter can be plugged to a controller.*/
	template <typename T> class RTParameter : public RTBase
	{
	private:
		/*The current value of the parameter*/
		T					_currentValue; 

		/*The source parameter (it's the output of a controller)*/
		RTParameter<T>*		_source; 

		/*Indicate if the parameter has to be recalculated*/
		bool				_dirty; 

		/*The controller owner of this parameter*/
		RTBaseController*	_parent; 

		/*Indicate if this parameter is the output of a controller or just a member*/
		bool				_output;

	public:

		//Constructor
		RTParameter();

		//Constructor for parameters
		RTParameter(RTSource<T>* parent, T value, bool output = false);

		//Constructor for output parameter
		RTParameter(RTSource<T>* parent);

		//Destructor
		~RTParameter();

		//initialisation function
		void init(RTBaseController* parent, RTParameter<T>* source, bool output = false);

		//Setter
		void setValue(const T& value);

		//Assignement operator
		const T&  operator= ( const T& );

		//Cast operator
		operator const T& ();

		//Connect a parameter to an other parameter
		void connectFrom(RTParameter<T>* source);

		//Connect a parameter to a controller
		void connectFrom(RTSource<T>* source);

		//Dirty the parameter
		void onChange();

		//calculate and return the parameter value
		const T& getValue();
	};

	//Constructor
	template <typename T> RTParameter<T>::RTParameter():_parent(0), _dirty(true), _output(false), _source(0)
	{}

	//Constructor for parameters
	template <typename T> RTParameter<T>::RTParameter(RTSource<T>* parent, T value, bool output = false)
	:_parent(parent), _currentValue(value), _dirty(true), _output(output), _source(0)
	{}

	//Constructor for output parameter
	template <typename T> RTParameter<T>::RTParameter(RTSource<T>* parent)
	:_parent(parent), _dirty(true), _output(true), _source(0)
	{}

	//Destructor
	template <typename T> RTParameter<T>::~RTParameter(){}

	//initialisation function
	template <typename T> void RTParameter<T>::init(RTBaseController* parent, RTParameter<T>* source, bool output = false)
	{
		_parent = parent;
		_output = output;
		_source = source;
	}

	//Setter
	template <typename T> void RTParameter<T>::setValue(const T& value)
	{
		_currentValue = value;
	}

	//calculate and return the parameter value
	template <typename T> const T& RTParameter<T>::getValue()
	{
		if (_dirty)
		{
			if ( _output && _parent != 0 )
				_parent->onDemand();
			else if ( _source != 0 )
				_currentValue = _source->getValue();
			_dirty = false;
		}
		return _currentValue;

	}

	//Assignement operator
	template <typename T> const T&  RTParameter<T>::operator= ( const T& value)
	{
		setValue(value);
		return getValue();
	}

	//Cast operator
	template <typename T> RTParameter<T>::operator const T& ()
	{
		return getValue();
	}

	//Connect a parameter to an other parameter
	template <typename T> void RTParameter<T>::connectFrom(RTParameter<T>* source)
	{
		_source = source;
	}

	//Connect a parameter to a controller
	template <typename T> void RTParameter<T>::connectFrom(RTSource<T>* source)
	{
		_source = source->getOutputRTParameter();
	}

	//Dirty the parameter
	template <typename T> void RTParameter<T>::onChange()
	{
		if (_dirty)
			return;

		_dirty = true;

		if(_parent != 0)
			_parent->onChange();
		if ( _source != 0)
			_source->onChange();

	}
}